<?php
class Location_Admin_Home_Controller extends Admin_Controller {

	public $restful = true;

    public function __construct()
    {
        parent::__construct();
        //--Your Code --//
        $this->theme->set_theme("admin") ;
    }

	public function get_index($pid=0){
        $data['pid'] = $pid;
        $data['parent'] = Locations::parent($pid)->get();

        $this->theme->title("Location List");
        return $this->theme->render('location::admin.index',$data);
	}

    public function get_child($pid=0){
        $data['pid'] = $pid;
        $data['parent'] = Locations::parent($pid)->get();
        $this->theme->title("Location List: Child");
        return $this->theme->render('location::admin.index',$data);
    }

    public function get_create($pid=0){

        $p = DB::table('locations')->where('id', '=', $pid)->first();

        if($p){
            $data['parent_name'] = $p->name;
            $data['parent_id'] = $p->id;
        }else{
            $data['parent_name'] = 'Root Location';
            $data['parent_id'] = 0;
        }

        //dd($data['parent_name']);
        $this->theme->title("Admin:Create Location");
        return $this->theme->render('location::admin.create',$data);
    }


    public function post_create($pid=0){


        $rules = array(
            'parent_id' => 'required|integer',
            'parent_name' => 'required',
            'name' => 'required|min:3|max:100',
        );

        $validator = Validator::make(Input::get(), $rules);

        if( $validator->fails() )
        {
            return Redirect::back()->with_input()->with_errors($validator)->with('message','Invalid Operation !');
        }

        else
        {
            $data = array(

                'parent_id' => Input::get('parent_id'),
                'name' => Input::get('name'),
                'slug' => Str::slug(Input::get('name')),
              //  'description' => Input::get('description'),
                'meta_title' => Input::get('meta_title'),
                'meta_keyword' => Input::get('meta_keyword'),
                'meta_description' => Input::get('meta_description')

            );

           // dd($data);
           
            Caches::clear('locations');
            
          

            Hierarchy::insert('locations',$data);

            //return Redirect::to('admin/home/child/'.Input::get('parent_id'));
            return Redirect::back()->with('message','Data Successfully inserted !');
        }

    }

    public function get_edit($pid){

        $p = DB::table('locations')->where('id','=',$pid)->first();

        $data['loc']['info'] = $p;

        if($p){

            $data['parent_name'] = $p->name;
            $data['parent_id'] = $p->id;
        }else{

            $data['parent_name'] = 'Root Category';
            $data['parent_id'] = 0;
        }

        $this->theme->title("Admin:Edit Location");

        return $this->theme->render('location::admin.edit',$data);
    }


    public function post_edit(){

        $rules = array(

            'parent_id' => 'required|integer',
            'parent_name' => 'required',
            'name' => 'required|min:3|max:100',
        );




        $validator = Validator::make(Input::get(), $rules);


        $thisOrderID = Input::get('locID');



        if( $validator->fails() )
        {
            return Redirect::back()->with_input()->with_errors($validator)->with('message','Invalid Operation !');
        }
        else
        {
        	
        	Caches::clear('locations');
        	
            /*Update Query : update Location info : by rahul(20/10/14) */
            $categories =DB::table('locations')
                ->where('id', '=', $thisOrderID)
                ->update(array(
                        'name' => Input::get('name'),
                        'slug' => Str::slug(Input::get('name') ),
                        'meta_title' =>Input::get('meta_title'),
                        'meta_keyword' =>Input::get('meta_keyword'),
                        'meta_description' => Input::get('meta_description'),

                    )
                );



            //return Redirect::to('admin/home/child/'.Input::get('parent_id'));
            return Redirect::back()->with('message','Data Successfully updated !');
        }

    }

    public function get_delete($pid){

        // Categories::find($pid)->delete();

        Hierarchy::delete('locations',$pid,true);

        //   DB::table('categories')->where('id', '=', $pid)->delete();
        return Redirect::back()->with('message','<div class="alert alert-info">Delete Successfully !<?');

    }

}
